package com.bailian.system.service.impl;

import com.bailian.common.core.text.Convert;
import com.bailian.common.utils.DateUtils;
import com.bailian.system.domain.BlPark;
import com.bailian.system.domain.BlParkTags;
import com.bailian.system.domain.BlParkConfig;
import com.bailian.system.domain.BlParkService;
import com.bailian.system.domain.BlWarehouseType;
import com.bailian.system.domain.vo.BlParkVo;
import com.bailian.system.mapper.BlParkMapper;
import com.bailian.system.mapper.BlParkTagsMapper;
import com.bailian.system.mapper.BlParkConfigMapper;
import com.bailian.system.mapper.BlParkServiceMapper;
import com.bailian.system.mapper.BlWarehouseTypeMapper;
import com.bailian.system.service.IBlParkService;
import com.bailian.system.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 园区信息Service业务层处理
 * 
 * @author bailian
 * @date 2024-01-15
 */
@Service
public class BlParkServiceImpl implements IBlParkService 
{
    @Autowired
    private BlParkMapper blParkMapper;
    
    @Autowired
    private BlParkTagsMapper parkTagsMapper;
    
    @Autowired
    private BlParkConfigMapper parkConfigMapper;
    
    @Autowired
    private BlParkServiceMapper parkServiceMapper;
    
    @Autowired
    private BlWarehouseTypeMapper warehouseTypeMapper;

    @Override
    public BlParkVo selectById(Long parkId)
    {
        BlPark park = blParkMapper.selectById(parkId);
        if (park == null) {
            return null;
        }
        
        // 完整的字段映射
        BlParkVo vo = new BlParkVo();
        vo.setParkId(park.getParkId());
        vo.setParkName(park.getParkName());
        vo.setParkDescription(park.getParkDescription());
        vo.setTotalArea(park.getTotalArea());
        vo.setBuildingArea(park.getBuildingArea());
        vo.setFloorHeight(park.getFloorHeight());
        vo.setLocation(park.getLocation());
        vo.setLongitude(park.getLongitude());
        vo.setLatitude(park.getLatitude());
        vo.setRentedArea(park.getRentedArea());
        vo.setRentalPrice(park.getRentalPrice());
        vo.setManager(park.getManager());
        vo.setContactPhone(park.getContactPhone());
        vo.setThumbnailUrl(park.getThumbnailUrl());
        vo.setPosterUrl(park.getPosterUrl());
        vo.setIsRecommend(park.getIsRecommend());
        vo.setAvailableArea(park.getAvailableArea());
        vo.setRentalRate(park.getRentalRate());
        vo.setContactEmail(park.getContactEmail());
        vo.setContactAddress(park.getContactAddress());
        vo.setAreaId(park.getAreaId());
        vo.setFireLevel(park.getFireLevel());
        vo.setBannerPage(park.getBannerPage());
        vo.setCreateTime(park.getCreateTime());
        vo.setUpdateTime(park.getUpdateTime());
        
        // 处理复杂字段：园区标签
        List<Long> tagIds = blParkMapper.selectParkTagIdsByParkId(parkId);
        if (tagIds != null && !tagIds.isEmpty()) {
            List<BlParkTags> tags = new ArrayList<>();
            for (Long tagId : tagIds) {
                BlParkTags tag = parkTagsMapper.selectBlParkTagsByTagId(tagId);
                if (tag != null) tags.add(tag);
            }
            vo.setParkTags(tags);
        }
        
        // 处理复杂字段：硬件配置
        List<Long> configIds = blParkMapper.selectParkConfigIdsByParkId(parkId);
        if (configIds != null && !configIds.isEmpty()) {
            List<BlParkConfig> configs = new ArrayList<>();
            for (Long configId : configIds) {
                BlParkConfig cfg = parkConfigMapper.selectBlParkConfigByConfigId(configId);
                if (cfg != null) configs.add(cfg);
            }
            vo.setHardwareConfig(configs);
        }
        
        // 处理复杂字段：园区服务
        List<Long> serviceIds = blParkMapper.selectParkServiceIdsByParkId(parkId);
        if (serviceIds != null && !serviceIds.isEmpty()) {
            List<BlParkService> services = new ArrayList<>();
            for (Long sid : serviceIds) {
                BlParkService svc = parkServiceMapper.selectBlParkServiceByServiceId(sid);
                if (svc != null) services.add(svc);
            }
            vo.setParkServices(services);
        }
        
        // 处理复杂字段：仓库类型
        // 优先从JSON字段转换（若存在）
        if (park.getWarehouseTypesJson() != null && !park.getWarehouseTypesJson().isEmpty()) {
            vo.setWarehouseTypes(JsonUtils.parseWarehouseTypes(park.getWarehouseTypesJson()));
        } else if (park.getWarehouseTypes() != null && !park.getWarehouseTypes().isEmpty()) {
            // 如果JSON字段为空，但实体对象中有数据，直接使用
            vo.setWarehouseTypes(park.getWarehouseTypes());
        }
        
        return vo;
    }

    @Override
    public List<BlParkVo> selectList(BlPark park)
    {
        List<BlPark> blParkList = blParkMapper.selectList(park);
        List<BlParkVo> voList = new ArrayList<>();
        for (BlPark p : blParkList) {
            BlParkVo vo = new BlParkVo();
            vo.setParkId(p.getParkId());
            vo.setParkName(p.getParkName());
            vo.setParkDescription(p.getParkDescription());
            vo.setTotalArea(p.getTotalArea());
            vo.setBuildingArea(p.getBuildingArea());
            vo.setFloorHeight(p.getFloorHeight());
            vo.setLocation(p.getLocation());
            vo.setLongitude(p.getLongitude());
            vo.setLatitude(p.getLatitude());
            vo.setRentedArea(p.getRentedArea());
            vo.setRentalPrice(p.getRentalPrice());
            vo.setManager(p.getManager());
            vo.setContactPhone(p.getContactPhone());
            vo.setThumbnailUrl(p.getThumbnailUrl());
            vo.setPosterUrl(p.getPosterUrl());
            vo.setIsRecommend(p.getIsRecommend());
            vo.setAvailableArea(p.getAvailableArea());
            vo.setRentalRate(p.getRentalRate());
            vo.setContactEmail(p.getContactEmail());
            vo.setContactAddress(p.getContactAddress());
            vo.setAreaId(p.getAreaId());
            vo.setFireLevel(p.getFireLevel());
            vo.setBannerPage(p.getBannerPage());
            vo.setCreateTime(p.getCreateTime());
            vo.setUpdateTime(p.getUpdateTime());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<BlParkVo> selectPage(BlPark park)
    {
        return selectList(park);
    }

    @Override
    public List<BlParkVo> selectListLightweight(BlPark park)
    {
        return selectList(park);
    }

    @Override
    public Map<String, Object> selectPageWithLimit(BlPark park, int pageNum, int pageSize)
    {
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("list", selectList(park));
        result.put("total", 0);
        return result;
    }

    @Override
    public List<BlParkVo> selectByConditions(Map<String, Object> conditions)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByTag(String tagName)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByWarehouseType(String warehouseType)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByHardwareConfig(String hardwareConfig)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByServiceType(String serviceType)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByRecommend(String isRecommend)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByPriceRange(BigDecimal minPrice, BigDecimal maxPrice)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByAreaRange(BigDecimal minArea, BigDecimal maxArea)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByLocation(String location)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectRecommendList(int limit)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectHotList(int limit)
    {
        return new ArrayList<>();
    }

    @Override
    public int insert(BlPark park)
    {
        park.setCreateTime(DateUtils.getNowDate());
        return blParkMapper.insert(park);
    }

    @Override
    public int insertBatch(List<BlPark> parkList)
    {
        int result = 0;
        for (BlPark park : parkList) {
            result += insert(park);
        }
        return result;
    }

    @Override
    public int update(BlPark park)
    {
        park.setUpdateTime(DateUtils.getNowDate());
        return blParkMapper.update(park);
    }

    @Override
    public int deleteById(Long parkId)
    {
        return blParkMapper.deleteById(parkId);
    }

    @Override
    public int deleteByIds(Long[] parkIds)
    {
        return blParkMapper.deleteByIds(parkIds);
    }

    @Override
    public int count(BlPark park)
    {
        return (int) blParkMapper.selectCount(park);
    }

    @Override
    public int countRecommend()
    {
        return 0;
    }

    @Override
    public List<Map<String, Object>> countByType()
    {
        return new ArrayList<>();
    }

    @Override
    public List<Map<String, Object>> countByLocation()
    {
        return new ArrayList<>();
    }

    @Override
    public Map<String, Object> getPriceStatistics()
    {
        return new java.util.HashMap<>();
    }

    @Override
    public Map<String, Object> getAreaStatistics()
    {
        return new java.util.HashMap<>();
    }

    @Override
    public int checkNameExists(String parkName, Long excludeId)
    {
        return 0;
    }

    @Override
    public int updateRecommendStatus(Long parkId, String isRecommend)
    {
        return 0;
    }

    @Override
    public int updateRentedArea(Long parkId, BigDecimal rentedArea)
    {
        return 0;
    }

    @Override
    public List<BlParkVo> selectByIds(Long[] parkIds)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByTags(String[] tags)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByWarehouseTypes(String[] warehouseTypes)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByHardwareConfigs(String[] hardwareConfigs)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByServiceTypes(String[] serviceTypes)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByAvailableArea(BigDecimal availableArea)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByRentalRate(BigDecimal rentalRate)
    {
        return new ArrayList<>();
    }

    @Override
    public List<Map<String, Object>> countByPriceRange()
    {
        return new ArrayList<>();
    }

    @Override
    public List<Map<String, Object>> countByAreaRange()
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectRecentlyUpdated(int limit)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectRecentlyCreated(int limit)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByComplexConditions(Long areaId, BigDecimal minPrice, BigDecimal maxPrice, BigDecimal minArea, BigDecimal maxArea, String isRecommend, List<String> warehouseTypes, List<Long> tagIds)
    {
        return new ArrayList<>();
    }

    @Override
    public List<BlParkVo> selectByComplexConditionsFromMap(Map<String, Object> conditions)
    {
        return new ArrayList<>();
    }

    @Override
    public void replaceParkConfigs(Long parkId, List<Long> parkConfigIds)
    {
        // 简化实现
    }

    @Override
    public void replaceParkServices(Long parkId, List<Long> serviceIds)
    {
        // 简化实现
    }

    @Override
    public void replaceParkTags(Long parkId, List<Long> tagIds)
    {
        // 简化实现
    }

    @Override
    public List<BlParkVo> selectByAreaId(Long areaId)
    {
        return new ArrayList<>();
    }

    @Override
    public int updateOtherParksToNotRecommend(Long areaId, Long excludeParkId)
    {
        return 0;
    }

    @Override
    public int smartUpdateRecommendStatus(Long parkId, String isRecommend)
    {
        return 0;
    }
}
